package com.spring.core.utils;

import com.spring.core.anotation.Controller;
import com.spring.core.anotation.Respotitory;
import com.spring.core.anotation.Service;

public class ReflectUtil {

    /**
     * 根据类对象获取首字母小写别名
     */
    private static String getLowerAlias(Class<?> cl) {
        String simpleName = cl.getSimpleName();
        String start =simpleName.substring(0,1).toLowerCase();
        String end = simpleName.substring(1);

        return  start + end;
    }

    /**
     * 获取到注解对应别名称
     * @param cl
     * @return
     */
    public static String getAnnotationAlias(Class<?> cl) {
        // 默认别名
        String aliasName = getLowerAlias(cl);

        if (cl.isAnnotationPresent(Respotitory.class)) {
        	Respotitory myRepository = cl.getAnnotation(Respotitory.class);

            if (!"".equals(myRepository.value())) {
                aliasName = myRepository.value();
            }
         } else if (cl.isAnnotationPresent(Service.class)) {
        	 Service service = cl.getAnnotation(Service.class);

            if (!"".equals(service.value())) {
                aliasName = service.value();
            }
        } else if (cl.isAnnotationPresent(Controller.class)) {
            Controller controller = cl.getAnnotation(Controller.class);

            if (!"".equals(controller.value())) {
                aliasName = controller.value();
            }
        }

        return aliasName;
    }

    /**
     * 定义处理url路径的方法
     * @param url
     * @return
     */
    public static String handlerUrl(String url) {
        if (url.startsWith("/")) {
            return url;
        } else {
            return  "/" + url;
        }
    }

}
